package com.hll.spring_book.service.category;

import com.hll.spring_book.mapper.category.CategoryMapper;
import com.hll.spring_book.pojo.Category;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2021-03-29 11:04:29
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
  @Resource private CategoryMapper categoryMapper;

  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Override
  public Category queryById(Integer id) {
    return this.categoryMapper.queryById(id);
  }

  /**
   * 新增数据
   *
   * @param category 实例对象
   * @return 实例对象
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      isolation = Isolation.DEFAULT,
      timeout = 36000,
      rollbackFor = Exception.class)
  @Override
  public Category insert(Category category) {
    this.categoryMapper.insert(category);
    return queryById(category.getId());
  }

  /**
   * 修改数据
   *
   * @param category 实例对象
   * @return 实例对象
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      isolation = Isolation.DEFAULT,
      timeout = 36000,
      rollbackFor = Exception.class)
  @Override
  public Category update(Category category) {
    this.categoryMapper.update(category);
    return this.queryById(category.getId());
  }

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 是否成功
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      isolation = Isolation.DEFAULT,
      timeout = 36000,
      rollbackFor = Exception.class)
  @Override
  public boolean deleteById(Integer id) {
    return this.categoryMapper.deleteById(id) > 0;
  }

  @Override
  public List<Category> queryAll(Category category) {
    return categoryMapper.queryAll(category);
  }

  @Override
  public boolean isCategoryExist(String name) {
    Category category = new Category();
    category.setName(name);
    return categoryMapper.queryAll(category).size() > 0;
  }
}
